﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace Pavilion.DesignerTool
{
    public class AnimationContainerViewModel : INotifyPropertyChanged
    {
        private AnimationResourceData _currentAnimationResource;
        public AnimationResourceData CurrentAnimationResource
        {
            get { return _currentAnimationResource; }
            set { _currentAnimationResource = value; OnPropertyChanged("CurrentAnimationResource"); }
        }
        public AnimationSelectionInfo AnimationSelectionInfo { get; private set; }
        public UndoRedoController UndeRedoController { get { return IsCurrentAnimationDataNull ? null : CurrentAnimationResource.CurrentAnimationLayer.CurrentAnimationData.UndoRedoController; } }
        private bool IsCurrentAnimationDataNull { get { return CurrentAnimationResource == null || CurrentAnimationResource.CurrentAnimationLayer == null || CurrentAnimationResource.CurrentAnimationLayer.CurrentAnimationData == null; } }

        public event PropertyChangedEventHandler PropertyChanged;

        public AnimationContainerViewModel()
        {
            AnimationSelectionInfo = new AnimationSelectionInfo();
        }

        public void LoadResourceItem(AnimationResourceData animatinResourceData)
        {
            CurrentAnimationResource = animatinResourceData;
            UpdateSelectionInfo();
        }

        public void UpdateSelectionInfo()
        {
            AnimationSelectionInfo.UpdateSelectionInfo(CurrentAnimationResource);
            OnPropertyChanged("UndeRedoController");
        }

        public void CreateAnimationLayer()
        {
            if (_currentAnimationResource == null)
                return;

            CurrentAnimationResource.CreateAnimationLayer();
        }

        public void RemoveAnimationLayer(AnimationLayer animationLayer)
        {
            CurrentAnimationResource.AnimationLayerCollection.Remove(animationLayer);
        }

        public void CreateFrame()
        {
            CurrentAnimationResource.CurrentAnimationLayer.CreateFrame();
        }

        public void RemoveFrame(FrameData frameData)
        {
            CurrentAnimationResource.CurrentAnimationLayer.RemoveFrame(frameData);
        }

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
